package com.ashafa.server;

import java.io.File;

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

import com.ashafa.fileutils.FetchFile;

public class PluginManager {
	
	Scriptable scope;
	String pluginsPath;
	
	
	public PluginManager(String pluginsPath, ScriptableObject scope){
		this.scope = scope;
		this.pluginsPath = pluginsPath;
		
	}
	
	public void importPlugin(String pluginName){
		Context cx = new CustomContextFactory().enterContext();
		try
		{
			File pluginFile = new File(pluginsPath + "/" + pluginName);
			if(pluginFile.exists()){
				cx.evaluateString(scope, FetchFile.readFile(pluginsPath+ "/" + pluginName, ""), pluginsPath+ "/" + pluginName +"", 1 , null);
			}
		} finally {
			Context.exit();	
		}
	}

}
